package ee.greaton.SMSsender;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		if (bundle != null) {
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++) {
				Message msg = new Message();
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				msg.from = msgs[i].getOriginatingAddress();
				msg.content = msgs[i].getMessageBody().toString();
				Calendar cal = Calendar.getInstance();
				msg.date = cal.get(Calendar.YEAR) + "." + (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.DAY_OF_MONTH);
				msg.time = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
				msg.addToReceivedDB();
				Toast.makeText(context, msg.toString(), Toast.LENGTH_SHORT).show();
			}
		}

	}
}
